﻿
Imports System.Data.SqlClient

Public Class frmJobOfferNew

    Sub CalculateSalary()
        Try
            Me.txtTotalSalary.Text = CDbl(Me.txtBS.Value + Me.txtMoveSal.Value + Me.txtHA.Value).ToString("N2")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub FillPositionsList()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From Position", cnn)
            Dim Reader As SqlDataReader

            Me.combJobTitle.Items.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.combJobTitle.Items.Add(Reader.Item("PositionAr"))
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub ButClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButClose.Click
        Me.Close()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Clear()
    End Sub

    Sub Clear()
        Me.txtEmpName.Clear()
        Me.txtNat.Clear()
        Me.txtPoB.Clear()
        Me.txtIDIssuePlace.Clear()
        Me.txtIDNo.Clear()
        Me.combJobTitle.SelectedItem = -1
        Me.combBranch.SelectedIndex = -1
        Me.txtNat.Clear()
        Me.txtBS.Value = 0
        Me.txtHA.Value = 0
        Me.txtMoveSal.Value = 0
        Me.txtTotalSalary.Clear()
        Me.DTPDateOfBirth.Value = Today
        Me.DTPIDExpDate.Value = Today
        Me.DTPIDIssueDate.Value = Today
        Me.txtInsuranceType.Clear()
        Me.txtVacationDays.Clear()
        Me.txtTickets.Clear()
        Me.txtEmpName.Focus()
    End Sub

    Private Sub txtBS_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBS.ValueChanged
        CalculateSalary()
    End Sub

    Private Sub txtMoveSal_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMoveSal.ValueChanged
        CalculateSalary()
    End Sub

    Private Sub txtHA_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtHA.ValueChanged
        CalculateSalary()
    End Sub

    Private Sub btnSave_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        ErrProv.Clear()
        If Me.txtEmpName.Text.Trim.Length = 0 Then
            ErrProv.SetError(txtEmpName, "Please Fill")
            Me.txtEmpName.Focus()

        ElseIf Me.combBranch.SelectedIndex = -1 Then
            ErrProv.SetError(combBranch, "Please Fill")
            Me.combBranch.Focus()

        ElseIf Me.combJobTitle.SelectedIndex = -1 Then
            ErrProv.SetError(combJobTitle, "Please Fill")
            Me.combJobTitle.Focus()

        Else
            Try
                Me.Cursor = Cursors.WaitCursor


                Dim Year, SNo As Integer

                Dim cmd As New SqlCommand
                Dim Trans As SqlTransaction

                cnn.Open()
                Trans = cnn.BeginTransaction
                cmd.Connection = cnn
                cmd.Transaction = Trans

                'Get Year
                cmd.CommandText = "Select Year(GetDate())"
                Year = CInt(cmd.ExecuteScalar)

                '1) built insert statement
                cmd.CommandText = "Insert Into JobOffers (EmpName,EmpDoB,EmpPob,Nationality,IDNo," & _
                                  "IDIssuePlace,IDIssueDate,IDExpDate,Branch,JobTitle,BasicSalary,HousingAllowance,TranspAllowance,TotalSalary,InsuranceType,VacationDays,AirTickets) VALUES (@EmpName,@EmpDoB,@EmpPob,@Nationality,@IDNo," & _
                                  "@IDIssuePlace,@IDIssueDate,@IDExpDate,@Branch,@JobTitle,@BasicSalary,@HousingAllowance,@TranspAllowance,@TotalSalary,@InsuranceType,@VacationDays,@AirTickets); SELECT @@IDENTITY;"

                'Assign parameters values
                cmd.Parameters.Clear()
                cmd.Parameters.AddWithValue("@EmpName", Me.txtEmpName.Text.Trim)
                cmd.Parameters.AddWithValue("@EmpDoB", Me.DTPDateOfBirth.Value)
                cmd.Parameters.AddWithValue("@EmpPob", Me.txtPoB.Text.Trim)
                cmd.Parameters.AddWithValue("@Nationality", Me.txtNat.Text.Trim)
                cmd.Parameters.AddWithValue("@IDNo", Me.txtIDNo.Text.Trim)
                cmd.Parameters.AddWithValue("@IDIssuePlace", Me.txtIDIssuePlace.Text.Trim)
                cmd.Parameters.AddWithValue("@IDIssueDate", Me.DTPIDIssueDate.Value)
                cmd.Parameters.AddWithValue("@IDExpDate", Me.DTPIDExpDate.Value)
                cmd.Parameters.AddWithValue("@Branch", Me.combBranch.Text)
                cmd.Parameters.AddWithValue("@JobTitle", Me.combJobTitle.Text)
                cmd.Parameters.AddWithValue("@BasicSalary", Me.txtBS.Text)
                cmd.Parameters.AddWithValue("@HousingAllowance", Me.txtHA.Text)
                cmd.Parameters.AddWithValue("@TranspAllowance", Me.txtMoveSal.Text)
                cmd.Parameters.AddWithValue("@TotalSalary", Me.txtTotalSalary.Text.Trim)
                cmd.Parameters.AddWithValue("@InsuranceType", Me.txtInsuranceType.Text.Trim)
                cmd.Parameters.AddWithValue("@VacationDays", Me.txtVacationDays.Text.Trim)
                cmd.Parameters.AddWithValue("@AirTickets", Me.txtTickets.Text.Trim)
                SNo = CInt(cmd.ExecuteScalar)
                Trans.Commit()
                cnn.Close()
                If MsgBox("Saved successfully !" & Chr(13) & " Offer number:  " & SNo & vbCrLf & "Do you want to print this offer ", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                    PrintJobOffer(SNo)
                End If
                Clear()
                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Private Sub Button2_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
        Me.ErrProv.Clear()
        If Me.txtEmpName.Text.Trim.Length = 0 Then
            Me.ErrProv.SetError(Me.txtEmpName, "Please fill in all details")
            Me.txtEmpName.Focus()

        ElseIf Me.combJobTitle.SelectedIndex = -1 Then
            Me.ErrProv.SetError(Me.combJobTitle, "Please fill in all details")
            Me.combJobTitle.Focus()

        ElseIf combBranch.SelectedIndex = -1 Then
            Me.ErrProv.SetError(Me.combBranch, "Please fill in all details")
            Me.combBranch.Focus()

        Else
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim das As New DsJobOffer

                das.Tables("JobOffers").Rows.Add({0, Me.txtEmpName.Text.Trim, Me.DTPDateOfBirth.Value, Me.txtPoB.Text.Trim, Me.txtNat.Text.Trim, _
                                                    Me.txtIDNo.Text.Trim, Me.txtIDIssuePlace.Text.Trim, Me.DTPIDIssueDate.Value, Me.DTPIDExpDate.Value, Me.combBranch.Text, _
                                                    Me.combJobTitle.Text, Me.txtBS.Value, Me.txtHA.Value, Me.txtMoveSal.Value, Me.txtTotalSalary.Text.Trim, _
                                                    Me.txtInsuranceType.Text.Trim, Me.txtVacationDays.Text.Trim, Me.txtTickets.Text.Trim})


                Dim rpt As New JopOffer
                rpt.SetDataSource(das)
                rptViewer.CrystalReportViewer1.ReportSource = rpt
                rptViewer.ShowDialog()

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub btnClear_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Clear()

    End Sub

    Private Sub ButClose_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButClose.Click
        Me.Close()

    End Sub

    'Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    '    ErrProv.Clear()
    '    If Me.txtEmpName.Text.Trim.Length = 0 Then
    '        ErrProv.SetError(txtEmpName, "Please Fill")
    '        Me.txtEmpName.Focus()

    '    ElseIf Me.combBranch.SelectedIndex = -1 Then
    '        ErrProv.SetError(combBranch, "Please Fill")
    '        Me.combBranch.Focus()

    '    ElseIf Me.combJobTitle.SelectedIndex = -1 Then
    '        ErrProv.SetError(combJobTitle, "Please Fill")
    '        Me.combJobTitle.Focus()

    '    Else
    '        Try
    '            Me.Cursor = Cursors.WaitCursor


    '            Dim Year, SNo As Integer

    '            Dim cmd As New SqlCommand
    '            Dim Trans As SqlTransaction

    '            cnn.Open()
    '            Trans = cnn.BeginTransaction
    '            cmd.Connection = cnn
    '            cmd.Transaction = Trans

    '            'Get Year
    '            cmd.CommandText = "Select Year(GetDate())"
    '            Year = CInt(cmd.ExecuteScalar)

    '            '1) built insert statement
    '            cmd.CommandText = "Insert Into JobOffers (EmpName,EmpDoB,EmpPob,IdenNo,Nationality," & _
    '                              "IdenSource,IdenDate,NatDate,JopTitle,JopPlace,BasicSal,HouAll,MovAll,InsDeg,InsType,VocCridit,AirTics,TotalSalary) VALUES (@EmpName,@EmpDoB,@EmpPob,@IdenNo,@Nationality," & _
    '                              "@IdenSource,@IdenDate,@NatDate,@JopTitle,@JopPlace,@BasicSal,@HouAll,@MovAll,@InsDeg,@InsType,@VocCridit,@AirTics,@TotalSalary); SELECT @@IDENTITY;"

    '            'Assign parameters values
    '            cmd.Parameters.Clear()
    '            cmd.Parameters.AddWithValue("@EmpName", Me.txtEmpName.Text.Trim)
    '            cmd.Parameters.AddWithValue("@EmpDoB", Me.DTPDateOfBirth.Value)
    '            cmd.Parameters.AddWithValue("@EmpPob", Me.txtPoB.Text.Trim)
    '            cmd.Parameters.AddWithValue("@IdenNo", Me.txtIDNo.Text.Trim)
    '            cmd.Parameters.AddWithValue("@Nationality", Me.txtNat.Text.Trim)
    '            'cmd.Parameters.AddWithValue("@Manag", Me.txtManag.Text.Trim)
    '            'cmd.Parameters.AddWithValue("@JopDeg", Me.cbJopDeg.Text)
    '            cmd.Parameters.AddWithValue("@IdenSource", Me.txtIDIssuePlace.Text.Trim)
    '            cmd.Parameters.AddWithValue("@IdenDate", Me.DTPIDExpDate.Value)
    '            cmd.Parameters.AddWithValue("@NatDate", Me.DTPIDIssueDate.Value)
    '            cmd.Parameters.AddWithValue("@JopTitle", Me.combJobTitle.Text)
    '            cmd.Parameters.AddWithValue("@JopPlace", Me.combBranch.Text)
    '            'cmd.Parameters.AddWithValue("@Section", Me.cbDept.Text)
    '            cmd.Parameters.AddWithValue("@BasicSal", Me.txtBS.Text)
    '            cmd.Parameters.AddWithValue("@HouAll", Me.txtHA.Text)
    '            cmd.Parameters.AddWithValue("@MovAll", Me.txtMoveSal.Text)
    '            'cmd.Parameters.AddWithValue("@OverSal", Me.txtOverall.Text)
    '            'cmd.Parameters.AddWithValue("@Commis", Me.txtCommissions.Text)
    '            'cmd.Parameters.AddWithValue("@InsDeg", Me.txtInsuranceDeg.Text.Trim)
    '            cmd.Parameters.AddWithValue("@InsType", Me.txtInsuranceType.Text.Trim)
    '            cmd.Parameters.AddWithValue("@VocCridit", Me.txtVacationDays.Text.Trim)
    '            cmd.Parameters.AddWithValue("@AirTics", Me.txtTickets.Text.Trim)
    '            cmd.Parameters.AddWithValue("@TotalSalary", Me.txtTotalSalary.Text.Trim)
    '            SNo = CInt(cmd.ExecuteScalar)
    '            Trans.Commit()
    '            cnn.Close()
    '            If MsgBox("تم حفظ عرض العمل  " & Chr(13) & " رقم العرض:  " & SNo & vbCrLf & " هل تريد ان تطبع عرض العمل؟", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
    '                PrintJobOffer(SNo)
    '            End If
    '            Clear()
    '            Me.Cursor = Cursors.Default
    '        Catch ex As Exception
    '            Me.Cursor = Cursors.Default
    '            If cnn.State = ConnectionState.Open Then
    '                cnn.Close()
    '            End If
    '            MsgBox(ex.Message)
    '        End Try
    '    End If
    'End Sub

    'Private Sub Button2_Click_3(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
    '    Me.ErrProv.Clear()
    '    If Me.txtEmpName.Text.Trim.Length = 0 Then
    '        Me.ErrProv.SetError(Me.txtEmpName, "Please fill in all details")
    '        Me.txtEmpName.Focus()

    '    ElseIf Me.combJobTitle.SelectedIndex = -1 Then
    '        Me.ErrProv.SetError(Me.combJobTitle, "Please fill in all details")
    '        Me.combJobTitle.Focus()

    '    ElseIf combBranch.SelectedIndex = -1 Then
    '        Me.ErrProv.SetError(Me.combBranch, "Please fill in all details")
    '        Me.combBranch.Focus()


    '    Else
    '        Try
    '            Me.Cursor = Cursors.WaitCursor

    '            Dim das As New DataSet

    '            das.Tables("JobOffers").Rows.Add({0, Me.txtEmpName.Text.Trim, Me.DTPDateOfBirth.Value, Me.txtPoB.Text.Trim, _
    '                                          Me.txtIDNo.Text.Trim, Me.txtNat.Text.Trim, Me.txtIDIssuePlace.Text.Trim, Me.DTPIDExpDate.Value, Me.DTPIDIssueDate.Value, Me.combJobTitle.Text, Me.combBranch.Text, Me.txtBS.Value, Me.txtHA.Value, Me.txtMoveSal.Value, Me.txtInsuranceType.Text.Trim, Me.txtVacationDays.Text.Trim, Me.txtTickets.Text.Trim, Me.txtTotalSalary.Text.Trim})


    '            Dim rpt As New JopOffer
    '            rpt.SetDataSource(das)
    '            rptViewer.CrystalReportViewer1.ReportSource = rpt
    '            rptViewer.ShowDialog()

    '            Me.Cursor = Cursors.Default
    '        Catch ex As Exception
    '            Me.Cursor = Cursors.Default
    '            If cnn.State = ConnectionState.Open Then
    '                cnn.Close()
    '            End If
    '            MsgBox(ex.ToString)
    '        End Try
    '    End If
    'End Sub

    Private Sub btnClear_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Clear()
    End Sub

    Private Sub ButClose_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButClose.Click
        Me.Close()
    End Sub

    Private Sub frmJobOfferNew_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.txtEmpName.Focus()
        FillPositionsList()
        Clear()
    End Sub

    Private Sub txtBS_ValueChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBS.ValueChanged
        CalculateSalary()

    End Sub

    Private Sub txtMoveSal_ValueChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMoveSal.ValueChanged
        CalculateSalary()
    End Sub

    Private Sub txtHA_ValueChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtHA.ValueChanged
        CalculateSalary()
    End Sub
End Class